package housekeeping.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import housekeeping.mapper.WorkerMapper;
import housekeeping.pojo.Worker;
import housekeeping.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author Du
 * @Date 2025/7/7 08:21
 * @注释
 */
@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    private WorkerMapper workerMapper;
    @Override
    public List<Worker> getWorkers() {
        List<Worker> list = workerMapper.selectList(null);
        return list;
    }

    @Override
    public void deleteWorker(Integer id) {
        int result = workerMapper.deleteById(id);
        if(result < 1){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public Worker selectById(Integer id){
        Worker worker = workerMapper.selectById(id);
        if(worker != null){
            return worker;
        }
        else{
            throw new RuntimeException("查询家政人员失败");
        }
    }

    @Override
    public List<Worker> selectByCategoryId(Integer categoryId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",categoryId);//查询条件
        List<Worker> workers = workerMapper.selectList(queryWrapper);
        return workers;
    }

    @Override
    public void updateWorker(Worker worker) {
//        Worker oldWorker = workerMapper.selectById(worker.getId());
//        if(oldWorker != null){
//            int result = workerMapper.updateById(worker);
            workerMapper.updateWithoutId(worker);
//            if(result < 1){
//                throw new RuntimeException("更新失败");
//            }
//        }
//        else{
//            throw new RuntimeException("更新失败");
//        }
    }

    @Override
    public List<Worker> selectByServiceType(String serviceType) {
        QueryWrapper<Worker> queryWrapper = new QueryWrapper();
        queryWrapper.eq("service_type",serviceType);//查询条件
        List<Worker> workers = workerMapper.selectList(queryWrapper);
        return workers;
    }

    @Override
    public Worker selectByAccount(String account) {
        QueryWrapper<Worker> queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",account);//查询条件
        Worker worker = workerMapper.selectOne(queryWrapper);
        return worker;
    }
}
